<template>
    <div>
        <ReporTable :ReportTable="ReportTable" />
        <div style="margin-top: 15px;" class="reporttable-btn">
            <el-button style="background-color: #2752fb;color: white;" @click="archive">归档{{
                yearMonth.slice(4) }}报表</el-button>
            <el-button style="background-color: #2752fb;color: white;" @click="createReport">新建报表</el-button>
            <el-button @click="$router.go(-1)">取消</el-button>
        </div>
    </div>
</template>

<script setup lang="ts">
defineProps(['ReportTable'])
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, nextTick } from 'vue'
import { useRoute } from 'vue-router'
import { addSocialTables, archiveSocialTables } from '@/api/Socialtable'
import ReporTable from './ReporTable.vue';
const route = useRoute()
const yearMonth: any = ref(route.query.yearMonth)
const year = yearMonth.value.substr(0, 4)
const month = Number(yearMonth.value.substr(4)) + 1
//社保数据归档
const archive = () => {
    ElMessageBox.confirm(
        '您确定要归档' + yearMonth.value + '的报表？报表归档将覆盖上一次归档记录无法恢复',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
        }
    )
        .then(() => {

            archiveSocialTables(yearMonth.value).then(res => {
                console.log(res);
                if (res.data.code == 10000) {
                    ElMessage({
                        type: 'success',
                        message: res.data.message,
                    })
                    return
                }
                ElMessage({
                    type: 'warning',
                    message: res.data.message,
                })
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消归档',
            })
        })
}
//制作新报表
const createReport = () => {
    ElMessageBox.confirm(
        '您将创建《' + year + '年' + month + '月》' + '的报表',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
        }
    )
        .then(() => {
            let y: any = ''
            setTimeout(() => {
                y = addSocialTables(year + '0' + month).then((res: any) => {
                    if (res.data.code == 10000) {
                        ElMessage({
                            type: 'success',
                            message: res.data.message,
                        })
                        return
                    }
                })
            }, 0)
            if (!y) {
                ElMessage({
                    type: 'warning',
                    message: '上个月数据还没有归档，请先进行归档操作',
                })
            }
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消创建',
            })
        })
}
</script>

<style scoped>
.reporttable-btn {
    margin-left: 450px;

    .el-button {
        border-radius: 0px;
    }
}
</style>